<?php

declare(strict_types=1);

namespace app\common\model;

class ProductSpec extends Base
{

    protected $name = 'product_spec';

    public function product()
    {
        return $this->hasOne(Product::class, 'id', 'pid')
            ->field('id,title,price,cate_1,cate_2,icon,stock,status,freight');
    }

    public static function getSettled($param, $user): array
    {
        try {
            $product = Product::where(['id' => $param['pid'], 'status' => 1])->find();

            if (empty($product)) {
                return ['code' => -1, 'msg' => '商品已下架'];
            }
            if (!$param['num'] || $param['num'] < 1) {
                return ['code' => -1, 'msg' => '购买数量错误'];
            }

            if ($product['restrict'] > 0) {
                $order_id = Order::where(['uid' => $user['id']])->where('status', '<', 7)->order('id desc')->column('id');
                $sum  = OrderProduct::whereIn('order_id', $order_id)->where('pid', $param['pid'])->sum('num');
                if (($sum + $param['num']) > $product['restrict']) {
                    return ['code' => -1, 'msg' => '该商品限购' . $product['restrict'] . '件'];
                }
            }
            if ($product->spec_open == 2) {
                $specValue = parent::where(['merge_title' => $param['spec'], 'pid' => $param['pid']])->find();
                if (empty($specValue)) {
                    return ['code' => -1, 'msg' => '未查询到相关规格'];
                }
                if ($param['num'] > $specValue->stock) {
                    return ['code' => -1, 'msg' => '商品库存不足'];
                }

                $data['product'] = [
                    [
                        'pid'       => $specValue->pid,
                        'title'     => $product->title,
                        'icon'      => $specValue->icon,
                        'spec'      => $param['spec'],
                        'vip_price' => $specValue->vip_price,
                        'num'       => $param['num'],
                        'cate_1'    => $product->cate_1
                    ]
                ];
                $data['total'] = $specValue->vip_price * $param['num'];
                $data['cate_1'] = [$product->cate_1];
            } else {
                if ($param['num'] > $product->stock) {
                    return ['code' => -1, 'msg' => '商品库存不足'];
                }
                $data['product'] = [
                    [
                        'pid'       => $product->id,
                        'title'     => $product->title,
                        'icon'      => $product->icon,
                        'vip_price' => $product->vip_price,
                        'spec'      => '',
                        'num'       => $param['num'],
                        'cate_1'    => $product->cate_1
                    ]
                ];
                $data['cate_1'] = [$product->cate_1];
                $data['total'] = $product->vip_price * $param['num'];
            }
            $data['freightTotal'] = $product->freight * $param['num'];
            $data['realTotal'] = $data['total'] + $data['freightTotal'];
            // print_r($data);die;
            return ['code' => 1, 'msg' => 'success', 'data' => $data];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => '服务器错误：' . $e->getMessage()];
        }
    }
}
